package com.controllers
{
	import com.dcliment.utils.DisplayObjectUtil;
	import com.events.AppEvent;
	import com.greensock.TweenMax;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	public class ArrowsController extends EventDispatcher
	{
		public static var ARROW_RIGHT:String = "arrow_right";
		public static var ARROW_LEFT:String = "arrow_left";
		//
		private var _view:Sprite;
		//
		private var _arrowR:MovieClip;
		private var _arrowL:MovieClip;
		//
		private var _popupController:PopupController;
		// ACCESSORS
		private var _totalPages:int;
		private var _arrowClicked:String;
		
		public function ArrowsController($view:Sprite, $controller:PopupController)
		{
			
			_view 				= $view;
			_popupController 	= $controller;
			
			// ---------------------
			// UI
			_arrowR = DisplayObjectUtil.getLibraryObject("ArrowR");
			_arrowR.x = 886;
			_arrowR.y = 326;
			_view.addChild(_arrowR);
			_arrowR.buttonMode = true;
			_arrowR.addEventListener(MouseEvent.CLICK, onRightClick, false, 0, true);
			
			_arrowL = DisplayObjectUtil.getLibraryObject("ArrowL");
			_arrowL.x = 86.8
			_arrowL.y = 326;
			_view.addChild(_arrowL);
			_arrowL.buttonMode = true;
			_arrowL.addEventListener(MouseEvent.CLICK, onLeftClick, false, 0, true);
			
		
		}
		
		//---------------------------
		// INIT
		public function init($totalPages):void
		{
			_arrowClicked = null;
			_totalPages = $totalPages;
			
			checkArrows();
		}
		
		private function checkArrows():void
		{
			if(_totalPages == 1)
			{
				_arrowL.visible = false;
				_arrowR.visible = false;
				
				return;
				
			}
			else
			{
				_arrowL.visible = true;
				_arrowR.visible = true;
			}
			
			
			// LEFT BUTTON
			if(_popupController.sequenceNum > 0)
				enable(_arrowL);
			else
				disable(_arrowL);
			
			//trace("TOTAL PAGES = " + _model.totalPages)
			
			// right arrow
			if(_totalPages == 1)
				disable(_arrowR);

			else if(_popupController.sequenceNum == _totalPages-1)
				disable(_arrowR);
			
			else
				enable(_arrowR);
				
			
		}
		private function enable($a:MovieClip):void
		{
			TweenMax.to($a, 0, {tint:null});
			$a.mouseEnabled = true;
		}
		private function disable($a:MovieClip):void
		{
			TweenMax.to($a, 0, {tint:0xB7B7B7});
			$a.mouseEnabled = false;
		}
		//------------------
		// EVENTS
		private function onRightClick(e:MouseEvent):void
		{
			_arrowClicked = ArrowsController.ARROW_RIGHT;
			
			_popupController.sequenceNum++;
			
			checkArrows();
		}
		private function onLeftClick(e:MouseEvent):void
		{
			_arrowClicked = ArrowsController.ARROW_LEFT;
			
			_popupController.sequenceNum--;
			
			checkArrows();
		}

		public function get arrowClicked():String
		{
			return _arrowClicked;
		}

		public function set arrowClicked(value:String):void
		{
			_arrowClicked = value;
		}

	}
}